'use strict'

/**
 * 根据id返回一个 dom对象或者多个对象的集合
 *
 * @param      {str}      id      合法的元素id
 * @return     {boolean}  { description_of_the_return_value }
 */
exports.$ = function(id) {
    if (!id instanceof String) {
        return false
    }
    let tar = id.substring(1)
    switch (id.charAt(0)) {
        case '.':
            return document.getElementsByClassName(tar)
            break;

        case '#':
            return document.getElementById(tar)
            break;

        default:
            return document.getElementsByTagName(id)
            break;
    }
}

/**
 * 判断是否数组
 *
 * @param      {<type>}   tar     The tar
 * @return     {boolean}  True if array, False otherwise.
 */
exports.isArray = function(tar) {
    return typeof(tar) === 'undefined' ? false : tar instanceof Array
}

/**
 * 直接打印目标对象，如果是数组，自动遍历
 *
 * @param      {<type>}  tar     The tar
 */
exports.print = function(tar) {
    console.log('typeof:', typeof(tar));
    if (tar instanceof Array) {
        console.log('当前是数组');
        let num = 0
        for (let each of tar) {
            console.log(num + '、' + each);
            num++
        }
        console.log(`共有${tar.length}个元素`);
    } else {
        console.log(tar);
    }
}

/**
 * 创建元素
 * @param      {<type>}  doc      The document
 * @param      {<type>}  htmlTag  The html tag
 * @param      {<type>}  value    The value
 */
exports.addElement = function(doc, htmlTag, value) {
    const tmpTag = document.createElement(htmlTag)
    if (typeof(value) !== 'undefined' || value !== undefined) {
        const tmpVal = document.createTextNode(value)
        tmpTag.appendChild(tmpVal)
    }

    doc.appendChild(tmpTag)
    return tmpTag
}

/**
 * 将 canvs 的数据 转换成 file jpeg数据
 *
 * @param      {<type>}  dataurl   The dataurl
 * @param      {string}  filename  The filename
 * @return     {File}    { description_of_the_return_value }
 */
exports.dataURLtoFile = function(dataurl, filename = 'file') {
    let arr = dataurl.split(',')
    let mime = arr[0].match(/:(.*?);/)[1]
    let suffix = mime.split('/')[1]
    let bstr = atob(arr[1])
    let n = bstr.length
    let u8arr = new Uint8Array(n)
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n)
    }
    return new File([u8arr], `${filename}.${suffix}`, {
        type: mime
    })
}

//mimetype对应文件类型字典
exports.mimetypes = {
    'text':'text/plain',
    'doc': 'application/msword',
    'bin': 'application/octet-stream',
    'exe': 'application/octet-stream',
    'so': 'application/octet-stream',
    'dll': 'application/octet-stream',
    'pdf': 'application/pdf',
    'ai': 'application/postscript',
    'xls': 'application/vnd.ms-excel',
    'ppt': 'application/vnd.ms-powerpoint',
    'dir': 'application/x-director',
    'js': 'application/x-javascript',
    'swf': 'application/x-shockwave-flash',
    'xhtml': 'application/xhtml+xml',
    'xht': 'application/xhtml+xml',
    'zip': 'application/zip',
    'mid': 'audio/midi',
    'midi': 'audio/midi',
    'mp3': 'audio/mpeg',
    'rm': 'audio/x-pn-realaudio',
    'rpm': 'audio/x-pn-realaudio-plugin',
    'wav': 'audio/x-wav',
    'bmp': 'image/bmp',
    'gif': 'image/gif',
    'jpeg': 'image/jpeg',
    'jpg': 'image/jpeg',
    'png': 'image/png',
    'css': 'text/css',
    'html': 'text/html',
    'htm': 'text/html',
    'txt': 'text/plain',
    'xsl': 'text/xml',
    'xml': 'text/xml',
    'mpeg': 'video/mpeg',
    'mpg': 'video/mpeg',
    'avi': 'video/x-msvideo',
    'movie': 'video/x-sgi-movie'
};

console.log('cps模块成功载入');